import 'dart:async';

import 'package:flutter/material.dart';
import 'package:provide/provide.dart';

import '../data_share/transfer_data_entity.dart';
import '../data_share/transfer_stream_singleton.dart';
import 'HeartProvide.dart';
import 'TowPages.dart';


class FirstPage extends StatefulWidget {
  const FirstPage({Key key}) : super(key: key);

  @override
  FirstPageState createState() => FirstPageState();
}

class FirstPageState extends State<FirstPage> {
  final StreamController _streamController = streamSingletonData.streamController;
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: const Text("第一个页面"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              '第一个页面' ,
              style: TextStyle(fontSize: 30.0),
            ),
            StreamBuilder(
              stream: _streamController.stream,
              initialData : TransferDataEntity(),
              builder: (context,data){
                return IconButton(
                  icon: data.data.isCheck
                      ? const Icon(Icons.favorite, size: 50)
                      : const Icon(Icons.favorite_border, size: 50),
                  onPressed: () {
                    setState(() {
                      data.data.isCheck = data.data.isCheck ? false : true;
                      print("data-first:${data.data}");
                      streamSingletonData.setTransferData(data.data);
                    });
                  },
                );
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          await Navigator.push(
            context,
            MaterialPageRoute(builder: (context) =>
                const SecondPage()
            ),
          );
          setState(() {
            print("更新UI......");
          });
        },
        child: const Icon(Icons.arrow_right),
      ),
    );
  }
}


class FirstProvidePage extends StatefulWidget {
  const FirstProvidePage({Key key}) : super(key: key);

  @override
  FirstProvidePageState createState() => FirstProvidePageState();
}

class FirstProvidePageState extends State<FirstProvidePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("第一个页面"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Provide<MyProvide>(
              builder: (context, child, provide) {
                return Text(
                  '阅读人数：' + '${provide.getNum}',
                  style: TextStyle(fontSize: 30.0),
                );
              },
            ),
            Provide<MyProvide>(
              builder: (context, child, provide) {
                return IconButton(
                  icon: Provide.value<MyProvide>(context).getIsFavorite
                      ? const Icon(Icons.favorite, size: 50)
                      : const Icon(Icons.favorite_border, size: 50),
                  onPressed: () {
                    Provide.value<MyProvide>(context).changeFavorite(
                        !Provide.value<MyProvide>(context).getIsFavorite);
                  },
                );
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => SecondProvidePage()),
          );
        },
        child: const Icon(Icons.arrow_right),
      ),
    );
  }
}